home *** CD-ROM | disk | FTP | other *** search
/ PC World 2007 December / PCWorld_2007-12_cd.bin / v cisle / htttrack / httrack-3.41-3.exe / {app} / src_win / WinHTTrack / HTTrackInterface.c < prev    next >
C/C++ Source or Header  |  2006-08-15  |  6KB  |  244 lines

  1. /* ------------------------------------------------------------ */
  2. /*
  3. HTTrack Website Copier, Offline Browser for Windows and Unix
  4. Copyright (C) Xavier Roche and other contributors
  5.  
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  19.  
  20.  
  21. Important notes:
  22.  
  23. - We hereby ask people using this source NOT to use it in purpose of grabbing
  24. emails addresses, or collecting any other private information on persons.
  25. This would disgrace our work, and spoil the many hours we spent on it.
  26.  
  27.  
  28. Please visit our Website: http://www.httrack.com
  29. */
  30.  
  31.  
  32. /* ------------------------------------------------------------ */
  33. /* File: Interface                                              */
  34. /* Author: Xavier Roche                                         */
  35. /* ------------------------------------------------------------ */
  36.  
  37. #include <sys/types.h>
  38. #include <sys/stat.h>
  39. #include <time.h>
  40.  
  41. //#include "htsglobal.h"
  42. //#include "htsbase.h"
  43.  
  44. #include "htsopt.h"
  45. #include "HTTrackInterface.h"
  46.  
  47. // Lecture d'une ligne (peut Ωtre unicode α priori)
  48. int linput(FILE* fp,char* s,int max) {
  49.   int c;
  50.   int j=0;
  51.   do {
  52.     c=fgetc(fp);
  53.     if (c!=EOF) {
  54.       switch(c) {
  55.         case 13: break;  // sauter CR
  56.         case 10: c=-1; break;
  57.         case 9: case 12: break;  // sauter ces caractΦres
  58.         default: s[j++]=(char) c; break;
  59.       }
  60.     }
  61.   }  while((c!=-1) && (c!=EOF) && (j<(max-1)));
  62.   s[j]='\0';
  63.   return j;
  64. }
  65. int linput_trim(FILE* fp,char* s,int max) {
  66.   int rlen=0;
  67.   char* ls=(char*) malloct(max+2);
  68.   s[0]='\0';
  69.   if (ls) {
  70.     char* a;
  71.     // lire ligne
  72.     rlen=linput(fp,ls,max);
  73.     if (rlen) {
  74.       // sauter espaces et tabs en fin
  75.       while( (rlen>0) && ((ls[max(rlen-1,0)]==' ') || (ls[max(rlen-1,0)]=='\t')) )
  76.         ls[--rlen]='\0';
  77.       // sauter espaces en dΘbut
  78.       a=ls;
  79.       while((rlen>0) && ((*a==' ') || (*a=='\t'))) {
  80.         a++;
  81.         rlen--;
  82.       }
  83.       if (rlen>0) {
  84.         memcpy(s,a,rlen);      // can copy \0 chars
  85.         s[rlen]='\0';
  86.       }
  87.     }
  88.     //
  89.     freet(ls);
  90.   }
  91.   return rlen;
  92. }
  93. int linput_cpp(FILE* fp,char* s,int max) {
  94.   int rlen=0;
  95.   s[0]='\0';
  96.   do {
  97.     int ret;
  98.     if (rlen>0)
  99.     if (s[rlen-1]=='\\')
  100.       s[--rlen]='\0';      // couper \ final
  101.     // lire ligne
  102.     ret=linput_trim(fp,s+rlen,max-rlen);
  103.     if (ret>0)
  104.       rlen+=ret;
  105.   } while((s[max(rlen-1,0)]=='\\') && (rlen<max));
  106.   return rlen;
  107. }
  108.  
  109. // idem avec les car spΘciaux
  110. void rawlinput(FILE* fp,char* s,int max) {
  111.   int c;
  112.   int j=0;
  113.   do {
  114.     c=fgetc(fp);
  115.     if (c!=EOF) {
  116.       switch(c) {
  117.         case 13: break;  // sauter CR
  118.         case 10: c=-1; break;
  119.         default: s[j++]=(char) c; break;
  120.       }
  121.     }
  122.   }  while((c!=-1) && (c!=EOF) && (j<(max-1)));
  123.   s[j++]='\0';
  124. }
  125.  
  126. // Like linput, but in memory (optimized)
  127. int binput(char* buff,char* s,int max) {
  128.   int count = 0;
  129.   int destCount = 0;
  130.  
  131.   // Note: \0 will return 1
  132.   while(destCount < max && buff[count] != '\0' && buff[count] != '\n') {
  133.     if (buff[count] != '\r') {
  134.       s[destCount++] = buff[count];
  135.     }
  136.         count++;
  137.   }
  138.   s[destCount] = '\0';
  139.  
  140.   // then return the supplemental jump offset
  141.   return count + 1;
  142.  
  143. int fexist(const char* s) {
  144.   struct stat st;
  145.   memset(&st, 0, sizeof(st));
  146.   if (stat(s, &st) == 0) {
  147.     if (S_ISREG(st.st_mode)) {
  148.       return 1;
  149.     }
  150.   }
  151.   return 0;
  152.  
  153. off_t fsize(const char* s) {
  154.   FILE* fp;
  155.   fp=fopen(s,"rb");
  156.   if (fp!=NULL) {
  157.     off_t i;
  158.     fseek(fp,0,SEEK_END);
  159.     i = ftell(fp);
  160.     fclose(fp);
  161.     return i;
  162.   } else
  163.     return -1;
  164. }
  165.  
  166. TStamp time_local(void) {
  167.   return ((TStamp) time(NULL));
  168. }
  169.  
  170. /* / et \\ en / */
  171. static char* __fslash(char* a) {
  172.   int i;
  173.   for(i=0;i<(int) strlen(a);i++)
  174.     if (a[i]=='\\')  // convertir
  175.       a[i]='/';
  176.   return a;
  177. }
  178.  
  179. char* fslash(char *catbuff,const char* a) {
  180.   return __fslash(concat(catbuff,a,""));
  181. }
  182.  
  183. // conversion minuscules, avec buffer
  184. char* convtolower(char* catbuff,const char* a) {
  185.   strcpybuff(catbuff, a);
  186.   hts_lowcase(catbuff);  // lower case
  187.     return catbuff;
  188. }
  189.  
  190. // conversion en minuscules
  191. void hts_lowcase(char* s) {
  192.   int i;
  193.   for(i=0;i<(int) strlen(s);i++)
  194.     if ((s[i]>='A') && (s[i]<='Z'))
  195.       s[i]+=('a'-'A');
  196. }
  197.  
  198. char* next_token(char* p,int flag) {
  199.   int detect=0;
  200.   int quote=0;
  201.   p--;
  202.   do {
  203.     p++;
  204.     if (flag && (*p=='\\')) {   // sauter \x ou \"
  205.       if (quote) {
  206.         char c='\0';
  207.         if (*(p+1)=='\\')
  208.           c='\\';
  209.         else if (*(p+1)=='"')
  210.           c='"';
  211.         if (c) {
  212.           char* tempo = malloc(strlen(p) + 2 + 2);
  213.           tempo[0]=c; tempo[1]='\0';
  214.           strcatbuff(tempo,p+2);
  215.           strcpybuff(p,tempo);
  216.           free(tempo);
  217.         }
  218.       }
  219.     }
  220.     else if (*p==34) {  // guillemets (de fin)
  221.       char* tempo = malloc(strlen(p) + 2);
  222.       tempo[0]='\0';
  223.       strcatbuff(tempo,p+1);
  224.       strcpybuff(p,tempo);   /* wipe "" */
  225.       p--;
  226.       /* */
  227.       quote=!quote;
  228.       /* */
  229.       free(tempo);
  230.     }
  231.     else if (*p==32) {
  232.       if (!quote)
  233.         detect=1;
  234.     }
  235.     else if (*p=='\0') {
  236.       p=NULL;
  237.       detect=1;
  238.     }
  239.   } while(!detect);
  240.   return p;
  241. }
  242.